package common

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

// Nexus配置
type NexusConfig struct {
	Username    string // 用户名
	Password    string // 密码
	MavenURL    string // Nexus仓库URL
	MvnPath     string // maven地址
	SettingPath string // setting文件的地址
	MavenRepo   string // 本地maven仓库地址
	UseMvn      bool   // 是否使用maven上传
	NpmURL      string // npm仓库URL
	NpmRepo     string // 本地npm仓库地址
	NpmRcPath   string // npmrcPath
	Concurrency int    // 并发数
	Timeout     int    // 超时时间（秒）
}

// loadConfig 加载properties格式配置文件
func LoadConfig(filePath string) (*NexusConfig, error) {
	file, err := os.Open(filePath)
	if err != nil {
		return nil, err
	}
	defer file.Close()

	config := &NexusConfig{}
	scanner := bufio.NewScanner(file)

	for scanner.Scan() {
		line := strings.TrimSpace(scanner.Text())
		// 跳过注释和空行
		if strings.HasPrefix(line, "#") || line == "" {
			continue
		}

		// 解析 key=value
		parts := strings.SplitN(line, "=", 2)
		if len(parts) != 2 {
			continue // 格式错误，跳过
		}

		key := strings.TrimSpace(parts[0])
		value := strings.TrimSpace(parts[1])
		// 根据键设置配置值
		switch key {
		case "nexus.username":
			config.Username = value
		case "nexus.password":
			config.Password = value
		case "nexus.maven.url":
			config.MavenURL = value
		case "nexus.maven.path":
			config.MvnPath = value
		case "nexus.maven.settingPath":
			config.SettingPath = value
		case "nexus.maven.repo":
			config.MavenRepo = value
		case "nexus.maven.userMvn":
			bTrue, _ := strconv.ParseBool(value)
			config.UseMvn = bTrue
		case "nexus.npm.url":
			config.NpmURL = value
		case "nexus.npm.repo":
			config.NpmRepo = value
		case "nexus.npm.rcPath":
			config.NpmRcPath = value
		case "nexus.concurrency":
			concurrency, err := strconv.Atoi(value)
			if err != nil {
				return nil, fmt.Errorf("无效的concurrency值: %s", value)
			}
			config.Concurrency = concurrency
		case "nexus.timeout":
			timeout, err := strconv.Atoi(value)
			if err != nil {
				return nil, fmt.Errorf("无效的timeout值: %s", value)
			}
			config.Timeout = timeout
		}
	}

	if err := scanner.Err(); err != nil {
		return nil, err
	}
	if err != nil {
		fmt.Println("加载config.properties配置失败: ", err)
		fmt.Println("配置文件示例: ")
		fmt.Println("\nexus.username=admin\nnexus.password=nexus123\nnexus.maven.url=http://127.0.0.1:8081/repository/maven-releases\nnexus.maven.path=D:\\\\PROGRA~1\\\\apache-maven-3.8.4\\\\bin\\\\mvn.cmd\nnexus.maven.settingPath=D:\\\\code\\\\mvn\\\\settings-ht706.xml\nnexus.maven.repo=D:\\.m2\\repository\nnexus.maven.userMvn=true\nnexus.npm.url=http://127.0.0.1:8081/repository/npm-hosted\nnexus.npm.repo=D:\\Administrator\\npmRepository\nnexus.concurrency=10\nnexus.timeout=30")
		return nil, err
	} else {
		fmt.Println("加载config.properties配置成功:")
		fmt.Println("nexus.username: ", config.Username)
		fmt.Println("nexus.password: ", config.Password)
		fmt.Println("nexus.maven.url: ", config.MavenURL)
		fmt.Println("nexus.maven.path: ", config.MvnPath)
		fmt.Println("nexus.maven.settingPath: ", config.SettingPath)
		fmt.Println("nexus.maven.repo: ", config.MavenRepo)
		fmt.Println("nexus.maven.userMvn: ", config.UseMvn)
		fmt.Println("nexus.npm.url: ", config.NpmURL)
		fmt.Println("nexus.npm.repo: ", config.NpmRepo)
		fmt.Println("Concurrency: ", config.Concurrency)
		fmt.Println("Timeout: ", config.Timeout)
	}
	return config, nil
}
